package cn.javase.base.io._08_字节数组流;

import org.junit.jupiter.api.Test;

import java.io.*;

public class _字节数组流测试 {

    @Test
    public void test() {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 是节点流，默认byte数组大小为32
        byteArrayOutputStream.write(1);
        byteArrayOutputStream.write(2);
        byteArrayOutputStream.write(3);
        byteArrayOutputStream.write(4);
        byte[] byteArray = byteArrayOutputStream.toByteArray(); // 转为byte数组
        for (byte b : byteArray) {
            System.out.print(b);
        }
    }

    @Test
    public void test2() throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 是节点流，默认byte数组大小为32
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeLong(3000L);
        objectOutputStream.writeBoolean(true);
        objectOutputStream.writeBoolean(false);
        objectOutputStream.writeUTF("人生如戏");
        objectOutputStream.flush();
        objectOutputStream.close();
        byte[] byteArray = byteArrayOutputStream.toByteArray(); // 转为byte数组
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        System.out.println(objectInputStream.readLong());
        System.out.println(objectInputStream.readBoolean());
        System.out.println(objectInputStream.readBoolean());
        System.out.println(objectInputStream.readUTF());
        objectInputStream.close();
    }

}
